package com.xx.comm.Exception;

import com.xx.comm.R;
import com.xx.comm.api.HttpEnum;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author xiaoDong
 * @version 1.0.0
 * @ClassName GlobalExeption.java
 * @Description 全局异常处理类
 * @createTime 2023年10月18日 09:40:00
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {
    /**
     * 校验参数异常
     * @param e
     * @param request
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handleMethodArgumentNotValid(MethodArgumentNotValidException e, HttpServletRequest request) {

        log.error("请求地址{" + request.getRequestURI() + "}," +
                "{" + request.getMethod() + "}请求参数有误"
                + e.getBindingResult().getFieldError().getDefaultMessage());
        //
        return R.fail(HttpEnum.BAD_REQUEST.getCode(),
                e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * 请求方式异常
     * @param e
     * @param request
     * @return
     */

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {

        log.error("请求地址{" + request.getRequestURI() + "}," + "不支持{" + request.getMethod() + "}请求");
        return R.fail(HttpEnum.BAD_METHOD.getCode(),HttpEnum.BAD_METHOD.getMessage());
    }

    //AccountExpiredException
    @ExceptionHandler(AccountExpiredException.class)
    public R accountExpired(AccountExpiredException e, HttpServletRequest request) {
        return R.fail(HttpServletResponse.SC_UNAUTHORIZED,"用户帐号已过期");
    }
    //UsernameNotFoundException
    @ExceptionHandler(UsernameNotFoundException.class)
    public R usernameNotFound(UsernameNotFoundException e, HttpServletRequest request) {
        return R.fail(HttpServletResponse.SC_UNAUTHORIZED,"用户不存在");
    }
    //BadCredentialsException
    @ExceptionHandler(BadCredentialsException.class)
    public R badCredentials(BadCredentialsException e, HttpServletRequest request) {
        return R.fail(HttpServletResponse.SC_UNAUTHORIZED,"用户名/密码错误");
    }
    //AccessDeniedException
    @ExceptionHandler(AccessDeniedException.class)
    public R accessDenied(AccessDeniedException ex, HttpServletRequest request) {
        log.error("Captured AccessDeniedException: {}", ex.getMessage(), ex);
        return R.fail(HttpServletResponse.SC_FORBIDDEN,"权限不足");
    }

    @ExceptionHandler(AuthenticationCredentialsNotFoundException.class)
    public R handleAuthenticationCredentialsNotFoundException(AuthenticationCredentialsNotFoundException ex) {
        return  R.fail( HttpServletResponse.SC_UNAUTHORIZED,"未认证");
    }

}
